/*
 * @Description: 档案分类reducer
 * @Author: Rfan
 * @Date: 2022-05-31 15:25:56
 * @LastEditTime: 2022-06-05 12:01:09
 */

import { delItemByIdArray, insertItem, updateItemById } from '@/utils/list';
import {
  ADD_CLASSIFY,
  DELETE_CLASSIFY,
  EDIT_CLASSIFY,
  SET_CLASSIFY_LIST,
  SET_SELECTED_ITEM,
  SET_SELECT_AT,
  SET_SELECT_FOND,
  SET_SELECT_VER_ID
} from '../constants';

export const ArchiveClassifyReducer = (state, action) => {
  switch (action.type) {
    // 设置选中的全宗
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    // 设置选中的档案类型
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的版本
     * @param {*} id 选中的版本
     */
    case SET_SELECT_VER_ID:
      return {
        ...state,
        selectVerId: action.id
      };

    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 加载元数据列表
     * @param {Array} data 表格数据
     */
    case SET_CLASSIFY_LIST: {
      return {
        ...state,
        classifyList: action.data
      };
    }

    /**
     * @description: 添加元数据条目
     */
    case ADD_CLASSIFY: {
      return {
        ...state,
        classifyList: insertItem(action.data, state.classifyList)
      };
    }

    /**
     * @description: 编辑元数据条目
     */
    case EDIT_CLASSIFY: {
      return {
        ...state,
        classifyList: updateItemById(action.data.id, action.data, state.classifyList)
      };
    }

    /**
     * @description: 删除元数据条目
     */
    case DELETE_CLASSIFY: {
      return {
        ...state,
        classifyList: delItemByIdArray(action.ids, state.classifyList)
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
